<template>
    <h2>用户管理</h2>
    <div style="text-align: left;">
        <el-button type="primary" @click="openAddUserDialog('添加用户')">添加用户</el-button>
    </div>
    <el-table :data="userList" style="width: 100%">
        <el-table-column prop="id" key="cust" label="用户编号" width="100" />
        <el-table-column prop="uname" label="用户名" width="100" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="edu" label="学历" />
        <el-table-column prop="age" label="年龄" />
        <el-table-column prop="title" label="部门" />
        <el-table-column fixed="right" label="操作" width="150">

            <template #default="scope">
                <el-button size="small" type="primary" @click="openEditUserDialog('修改信息', scope.row)">
                    修改
                </el-button>

                <el-button size="small" type="danger" @click="handleDelete(scope.row)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination layout="prev, pager, next" :total="total" @current-change="getUserList"
        v-model:current-page="currentPage" />


    <el-dialog v-model="dialogUserVisible" :title="userDialogTitle" width="500">
        <el-form :model="userForm" label-width="120px">
            <el-form-item label="用户名">
                <el-input v-model="userForm.uname" style="width: 80%" />
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="userForm.phone" style="width: 80%" />
            </el-form-item>
            <el-form-item label="学历">
                <el-input v-model="userForm.edu" style="width: 80%" />
            </el-form-item>
            <el-form-item label="年龄">
                <el-input v-model="userForm.age" style="width: 80%" />
            </el-form-item>
            <el-form-item label="部门">
                <el-input v-model="userForm.title" style="width: 80%" />
            </el-form-item>

            <el-form-item label="角色">
                <el-select v-model="userForm.rids" placeholder="请选择角色...." style="width: 80%" multiple>
                    <el-option v-for="opt in optRoles" :label="opt.name" :value="opt.id" :key="opt.id" />

                </el-select>
            </el-form-item>

        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogUserVisible = false">取消</el-button>
                <el-button type="primary" @click="subUserForm">
                    保存
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';

const optRoles = ref([])
var url = ""

const userForm = reactive({
    uname: '',
    phone: '',
    age: '',
    edu: '',
    title: '',
    rids: []
})

const total = ref()
const currentPage = ref(1)

const userList = ref([])
const userDialogTitle = ref()
const dialogUserVisible = ref(false)

function openAddUserDialog(title) {
    url = "http://localhost:8081/addUser"
    userDialogTitle.value = title
    dialogUserVisible.value = true

    axios.get("http://localhost:8081/allRoles")
        .then((resp) => {
            optRoles.value = resp.data
        }).catch((error) => {
            console.log(error)
        })
}

function openEditUserDialog(title, row) {
    userDialogTitle.value = title
    dialogUserVisible.value = true

    userForm.age = row.age
    userForm.id = row.id;
    userForm.edu = row.edu;
    userForm.uname = row.uname;
    userForm.phone = row.phone;
    userForm.title = row.title;
    url = "http://localhost:8081/updateUser"

    axios.get("http://localhost:8081/allRoles")
        .then((resp) => {
            optRoles.value = resp.data
            axios.get("http://localhost:8081/queryUserRids/" + row.id)
                .then((resp => {
                    //将响应的角色id的集合赋值给表单的数组
                    userForm.rids = resp.data;
                }))
        }).catch((error) => {
            console.log(error)
        })
}

function handleDelete(row) {
    axios.get("http://localhost:8081/delUser/" + row.id)
        .then((resp) => {
            if (resp.data.code == 200) {
                getUserList(currentPage.value)
                ElMessage({ message: resp.data.msg, type: 'success' })
            } else {
                ElMessage.error(resp.data.msg)
            }
        }).catch((error) => {
            console.log(error);
        })
}

function subUserForm() {
    axios.post(url, userForm)
        .then((resp) => {
            if (resp.data.code == 200) {
                //关闭对话框
                dialogUserVisible.value = false
                getUserList(currentPage.value)
                ElMessage({ message: resp.data.msg, type: 'success' })
            } else {
                ElMessage.error(resp.data.msg)
            }
        })
        .catch((error) => {
            console.log(error);
        });
}

function getUserList(current) {
    axios.get("http://localhost:8081/listUsers?pageNum=" + current)
        .then((resp) => {
            userList.value = resp.data.list
            total.value = resp.data.total
        })
}

onMounted(() => {
    getUserList(1)
})
</script>

<style scoped></style>